<template>
  <div>
    <el-card shadow="hover">
      <div slot="header" class="clearfix">
        <span>{{ title }}</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="fetchData">刷新</el-button>
      </div>
      <el-row :gutter="24" style="margin-bottom: 5px; margin-left: 0px;">
        <el-radio-group v-model="queryParam.ticketPhase" size="mini" @change="fetchData">
          <el-radio-button :label="ticketPhase.ALL">全部</el-radio-button>
          <el-radio-button :label="ticketPhase.NEW">{{ ticketPhase.NEW | toPhaseText }}</el-radio-button>
          <el-radio-button :label="ticketPhase.TOAUDIT">{{ ticketPhase.TOAUDIT | toPhaseText }}</el-radio-button>
          <el-radio-button :label="ticketPhase.REJECT">{{ ticketPhase.REJECT | toPhaseText }}</el-radio-button>
          <el-radio-button :label="ticketPhase.SUCCESS">{{ ticketPhase.SUCCESS | toPhaseText }}</el-radio-button>
          <el-radio-button :label="ticketPhase.FAILED">{{ ticketPhase.FAILED | toPhaseText }}</el-radio-button>
          <el-radio-button :label="ticketPhase.CLOSED">{{ ticketPhase.CLOSED | toPhaseText }}</el-radio-button>
        </el-radio-group>
        <el-select v-model.trim="queryParam.workOrderId" filterable clearable @change="fetchData" placeholder="请选择工单类型">
          <el-option
            v-for="item in workOrderOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            <span style="float: left"><i :class="item.icon" style="margin-right: 5px"></i>{{ item.name }}</span>
          </el-option>
        </el-select>
      </el-row>
      <el-table :data="table.data" style="width: 100%" v-loading="table.loading">
        <el-table-column prop="id" label="编号" width="80"></el-table-column>
        <el-table-column prop="workorder" label="工单">
          <template slot-scope="scope">
            <i :class="scope.row.workOrder.icon" style="margin-right: 5px"></i>{{ scope.row.workOrder.name }}
          </template>
        </el-table-column>
        <el-table-column prop="createUser" label="申请人">
          <template slot-scope="scope">
            <user-avatar :user="scope.row.createUser" :size="24"></user-avatar>
          </template>
        </el-table-column>
        <el-table-column prop="ticketPhase" label="进度" width="120">
          <template slot-scope="scope">
            <el-tag class="filters" :type="scope.row.ticketPhase | toPhaseType" size="mini">
              <i class="el-icon-loading" v-show="scope.row.ticketPhase === 'PROCESSING'"></i>
              {{ scope.row.ticketPhase | toPhaseText }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="ago" label="申请时间" width="120"></el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button v-if="scope.row.ticketPhase !== 'NEW' && !scope.row.isApprover"
                       type="success"
                       plain size="mini"
                       :loading="scope.row.loading"
                       :disabled="disabled"
                       @click="previewTicket(scope.row)">查看
            </el-button>
            <el-button v-if="scope.row.ticketPhase === 'NEW'"
                       type="primary"
                       plain size="mini"
                       :loading="scope.row.loading"
                       :disabled="disabled"
                       @click="editTicket(scope.row)">编辑
            </el-button>
            <el-button v-if="scope.row.isApprover"
                       type="primary"
                       plain size="mini"
                       :loading="scope.row.loading"
                       :disabled="disabled"
                       @click="approvalTicket(scope.row)">审批
            </el-button>
            <el-button v-if="isAdmin"
                       type="danger"
                       plain size="mini"
                       @click="delTicket(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination :pagination="table.pagination" @paginationCurrentChange="paginationCurrentChange"
                  @handleSizeChange="handleSizeChange"></pagination>
    </el-card>
  </div>
</template>

<script>

import { toPhaseText, toPhaseType } from '@/filters/ticket.js'
import { GET_WORK_ORDER_OPTIONS } from '@/api/modules/workorder/workorder.api.js'
import {
  GET_WORK_ORDER_TICKET_VIEW,
  QUERY_WORK_ORDER_TICKET_PAGE,
  QUERY_MY_WORK_ORDER_TICKET_PAGE, DELETE_WORK_ORDER_TICKET_BY_ID
} from '@/api/modules/workorder/workorder.ticket.api.js'
import UserAvatar from '@/components/opscloud/workorder/child/UserAvatar'
import ticketFormStatus from '@/components/opscloud/workorder/child/ticket.form'
import Pagination from '@/components/opscloud/common/page/Pagination'
import WorkOrderKeyConstants from '@/components/opscloud/common/enums/workorder.key.constants'
import WorkOrderTicketPhase from '@/components/opscloud/common/enums/workorder.ticket.phase'

export default {
  name: 'MyTicketCard',
  props: {
    title: {
      type: String,
      required: false,
      default: '我的工单'
    },
    isAdmin: {
      type: Boolean,
      required: false,
      default: false
    }
  },
  data () {
    return {
      table: {
        data: [],
        loading: false,
        pagination: {
          currentPage: 1,
          pageSize: 10,
          total: 0
        }
      },
      ticketPhase: WorkOrderTicketPhase,
      workOrderKeyConstants: WorkOrderKeyConstants,
      workOrderOptions: [],
      formStatus: {
        ticket: Object.assign({}, ticketFormStatus.ticket)
      },
      queryParam: {
        extend: true,
        workOrderId: '',
        ticketPhase: ''
      },
      disabled: false
    }
  },
  computed: {},
  mounted () {
    this.fetchData()
    this.getWorkOrderOptions()
  },
  components: {
    Pagination,
    UserAvatar
  },
  filters: {
    toPhaseText, toPhaseType
  },
  methods: {
    paginationCurrentChange (currentPage) {
      this.table.pagination.currentPage = currentPage
      this.fetchData()
    },
    handleSizeChange (size) {
      this.table.pagination.pageSize = size
      this.fetchData()
    },
    getWorkOrderOptions () {
      GET_WORK_ORDER_OPTIONS().then(res => {
        this.workOrderOptions = res.body
      })
    },
    delTicket (ticket) {
      this.$confirm('此操作将删除当前工单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.table.loading = true
        DELETE_WORK_ORDER_TICKET_BY_ID(ticket.id).then(res => {
          this.$message.success('删除成功!')
          this.fetchData()
        })
      }).catch(() => {
        this.$message.info('已取消删除!')
      })
    },
    approvalTicket (row) {
      row.loading = true
      this.disabled = true
      GET_WORK_ORDER_TICKET_VIEW(row.id).then(res => {
        const ticket = res.body
        this.$emit('approvalTicket', ticket)
        row.loading = false
        this.disabled = false
      }).catch(() => {
        row.loading = false
        this.disabled = false
      })
    },
    previewTicket (row) {
      row.loading = true
      this.disabled = true
      GET_WORK_ORDER_TICKET_VIEW(row.id).then(res => {
        const ticket = res.body
        this.$emit('previewTicket', ticket)
        row.loading = false
        this.disabled = false
      }).catch(() => {
        row.loading = false
        this.disabled = false
      })
    },
    editTicket (row) {
      row.loading = true
      this.disabled = true
      GET_WORK_ORDER_TICKET_VIEW(row.id).then(res => {
        const ticket = res.body
        this.$emit('editTicket', ticket)
        row.loading = false
        this.disabled = false
      }).catch(() => {
        row.loading = false
        this.disabled = false
      })
    },
    fetchData () {
      this.table.loading = true
      const requestBody = {
        ...this.queryParam,
        page: this.table.pagination.currentPage,
        length: this.table.pagination.pageSize
      }
      if (this.isAdmin) {
        QUERY_WORK_ORDER_TICKET_PAGE(requestBody)
          .then(res => {
            this.table.data = res.body.data
            this.table.pagination.total = res.body.totalNum
            this.table.loading = false
          })
      } else {
        QUERY_MY_WORK_ORDER_TICKET_PAGE(requestBody)
          .then(res => {
            this.table.data = res.body.data
            this.table.pagination.total = res.body.totalNum
            this.table.loading = false
          })
      }
    }
  }
}
</script>

<style scoped>

>>> .el-card__header {
  padding: 10px 10px;
  border-bottom: 1px solid #EBEEF5;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

.el-select {
  margin-left: 10px;
}
</style>
